<?php
class Ecore_Search extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		
	}
	public function getMap($dblLat, $dblLng, $intRadius, $intLimit, &$intTotal=0) {
		try {
			$this->prepare('call searchs_nearby(?,?,?,?);');
			$this->bindParam(1, $dblLat		, PDO::PARAM_INT);
			$this->bindParam(2, $dblLng		, PDO::PARAM_INT);
			$this->bindParam(3, $intRadius	, PDO::PARAM_INT, 6);
			$this->bindParam(4, $intLimit	, PDO::PARAM_INT, 6);
			$arrResult = $this->fetchAll();
			$intTotal  = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_Search::getMap - ' .$e->getMessage());	
		}
	}
	public function getAll($intOffset=0, $intLimit, $intPage=1, $intType=0, $intCategoryId=0, $strAddr='', $strKeyword='', &$intTotal=-1) {
		try {			
			$this->prepare('call searchs_getall(?,?,?,?,?,
												?,?,?);');
			$this->bindParam(1, $intPage		, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intType		, PDO::PARAM_INT, 6);
			$this->bindParam(3, $intCategoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(4, $strAddr		, PDO::PARAM_STR, 150);			
			$this->bindParam(5, $strKeyword		, PDO::PARAM_STR, 50);									
			$this->bindParam(6, $intOffset		, PDO::PARAM_INT, 6);
			$this->bindParam(7, $intLimit		, PDO::PARAM_INT, 6);
			$this->bindParam(8, $intTotal		, PDO::PARAM_INT, 4);			
			$arrResult = $this->fetchAll();            
			if($intTotal == 0) {
				$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Ecore_Search::getAll - ' .$e->getMessage());	
		}
	}
	public function addUpdate($intId, $intType, $strName, $strAddr, $strTag,
							   $strShort, $strImage, $strSlug, $dbLng, $dnLat,
							   $strPostdate, $intStatus, $intPage, $intCategoryId, $strSub,
							   $intDistrict=0, $intProvince=0, $strCountry='VN', $intUserId=0) {
		try {					
			$this->prepare('call search_add_update(?,?,?,?,?,
												   ?,?,?,?,?,
												   ?,?,?,?,?,
												   ?,?,?,?);');
			$this->bindParam(1, $intId			, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intType		, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName		, PDO::PARAM_STR, 255);
			$this->bindParam(4, $strAddr		, PDO::PARAM_STR, 255);
			$this->bindParam(5, $strTag			, PDO::PARAM_STR, 100);
			$this->bindParam(6, $strShort		, PDO::PARAM_STR, 1000);
			$this->bindParam(7, $strImage		, PDO::PARAM_STR, 150);
			$this->bindParam(8, $strSlug		, PDO::PARAM_STR, 150);
			$this->bindParam(9, $dbLng			, PDO::PARAM_STR,20);
			$this->bindParam(10, $dnLat			, PDO::PARAM_STR,20);
			$this->bindParam(11, $strPostdate	, PDO::PARAM_STR, 20);
			$this->bindParam(12, $intStatus		, PDO::PARAM_INT, 4);
			$this->bindParam(13, $intPage		, PDO::PARAM_INT, 4);
			$this->bindParam(14, $intCategoryId	, PDO::PARAM_INT, 11);
			$this->bindParam(15, $strSub		, PDO::PARAM_STR, 100);
			$this->bindParam(16, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(17, $strCountry	, PDO::PARAM_STR, 2);
			$this->bindParam(18, $intProvince	, PDO::PARAM_INT, 11);
			$this->bindParam(19, $intDistrict	, PDO::PARAM_INT, 11);
			$this->execute();
		}catch(Exception $e) {
			Ecore_Function::logDb('Ecore_Search::addUpdate - ' .$e->getMessage());			
			
		}		
	}	
	public function updateStatus($intId, $intType, $intStatus) {
		try {					
			$this->prepare('call searchs_update_status(?,?,?);');
			$this->bindParam(1, $intId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intType	, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $intStatus	, PDO::PARAM_INT, 4);;
			$this->execute();
		}catch(Exception $e) {
			Ecore_Function::logDb('Ecore_Search::updateStatus - ' .$e->getMessage());			
			
		}		
	}
}